<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    class="pop-table"
    :closable="false">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" style="margin:30px 0">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发起人">
           <a-input placeholder="请输入发起人姓名" v-decorator="['authorName', validatorRules.authorId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主题名称">
          <a-input placeholder="请输入主题名称"
                   v-decorator="['themeName', {rules: [{ required: true, message: '请输入主题名称' }]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主题简介">
          <a-textarea v-decorator="['themeIntroduce', {}]" placeholder="请输入主题简介" :rows="4"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主题宣传图">
          <j-image-upload ref="imageUp" v-model="model.mbLogoUrl" @change="imgChange"></j-image-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主题内容">
          <a-textarea v-decorator="['content', {}]" placeholder="请输入主题内容" :rows="4"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="搜索关键词">
          <a-input placeholder="请输入搜索关键词" v-decorator="['keyword', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction, getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: 'ThmThemeInfoModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16}
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          authorId: {rules: [{required: true, message: '请输入发起人姓名!'}]}
        },
        url: {
          add: '/them/thmThemeInfo/add',
          edit: '/them/thmThemeInfo/edit'
        },
        imgList: [],
        mbLogoUrl: '',
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        // getAction('/content/conAuthorInfo/list').then((res) => {
        //   if (res.result.records) {
        //     this.authorList = res.result.records
        //   }
        // })
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.mbLogoUrl = record.mbLogoUrl
          this.$refs.imageUp.initFileList(record.mbLogoUrl)
          this.form.setFieldsValue(pick(this.model, 'remark', 'delFlag', 'categoryId', 'categoryName', 'authorId', 'authorHead', 'authorName', 'themeName', 'themeIntroduce', 'mbLogoUrl', 'content', 'keyword', 'isRelease', 'isTop', 'sort', 'readNum', 'collectionNum', 'forwardNum', 'thumbsNum', 'collectNum', 'shareNum', 'field1', 'field2', 'field3', 'field4', 'field5', 'field6'))
          // 时间格式化
          this.form.setFieldsValue({releaseTime: this.model.releaseTime ? moment(this.model.releaseTime) : null})
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            // 时间格式化
            // formData.releaseTime = formData.releaseTime?formData.releaseTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.mbLogoUrl = this.mbLogoUrl
            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      imgChange(path, item) {
        this.mbLogoUrl = path
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
